
local List = pluginRequire "common/libraries/List.lua"
-- local TableShow = pluginRequire "common/libraries/table_show.lua"

local Profile = {}

local profileQueue = List.new()
function Profile.profileLine(line)
	List.pushBack(profileQueue, {line, os.clock()})
end

function Profile.printProfile()
	local lines = {}
	local previousTime = nil
	
	while not List.empty(profileQueue) do
	
		local profileEvent = List.popFront(profileQueue)
		
		local line = lines[profileEvent[1]]
		
		if nil == line then
			line = {}
			line.count = 0
			line.elapsedTime = 0
		end
		
		local elapsedTime = nil
		if nil ~= previousTime then
			elapsedTime = profileEvent[2] - previousTime
		end
		previousTime = profileEvent[2]
		
		if nil ~= elapsedTime then
			line.count = line.count + 1
			line.elapsedTime = line.elapsedTime + elapsedTime
		end
		
		lines[profileEvent[1]] = line
	end
	
--	Logger:log("lines: " .. TableShow.show(lines))
	
	for line, lineInfo in pairs(lines) do
		Logger:log(line .. " - Count: " .. lineInfo.count .. " Time: " .. lineInfo.elapsedTime)
	end
end

return Profile